package BeenChina.PM.Action;
import java.util.ArrayList;
import java.util.List; 
import BeenChina.PM.Model.ModelRole;
import BeenChina.PM.Model.ModelUser; 
import BeenChina.PM.Model.ModelUserRole;
import BeenChina.PM.Service.ServiceFactory; 
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
 

public class UserInfoModifyAction extends ActionSupport{
	
	private String userName,userID,userPass ,id;
	private String oper;
	private String roles;
	
	public void setId(String value)
	{
		id = value;
	}
	public void setOper(String value)
	{
		oper = value;
	}
	
	public void setUserName(String value)
	{ userName = value;}
	
	public void setUserID(String value)
	{ userID = value;}
	
	public void setUserPass(String value)
	{ userPass = value;}
	
	public String getRoles()
	{ return roles;}
	public void setRoles(String value)
	{ this.roles = value; }
	
	private static final long serialVersionUID = 1L;
	
	 
	public String execute() {
		  
	 String result = ActionSupport.SUCCESS;	
	  try {
			
		
		  if(oper != null && oper.equals("edit")){   
			   
			 updateUser();
			  
		  }
		  else if (oper != null && oper.equals("add")){  
		        
			  if (existUser()>0)
			  {
				  result = ActionSupport.ERROR;
			  }
			  else {
				  insetUser();
			   }
		  }
		  else  if(oper != null && oper.equals("del")){   
			   
			  deleteUser();			  
		  }
		}
		catch (Exception ex)
		{
		    ex.printStackTrace();
		    result = ActionSupport.ERROR;
		}
	  
		 return result;
	 }

	private Integer insetUser()
	 {
		 ModelUser modelUser = new ModelUser(userName,userID,userPass);
		 
		 saveUserRoles();
		 
		 return ServiceFactory.getInstance().getSystemInfoService().insertUser(modelUser);
		 
	 }
	 
	private Integer existUser()
	 {
		 return ServiceFactory.getInstance().getSystemInfoService().existUser(userID);
	 }
	 
	private Integer updateUser()
	 {
		 ModelUser modelUser = new ModelUser(userName,id,userPass);
		 saveUserRoles();
		 return ServiceFactory.getInstance().getSystemInfoService().updateUser(modelUser); 
	 }
	 
	private Integer deleteUser()
	 {
		ServiceFactory.getInstance().getSystemInfoService().deleteUserRoles(id);
		 return ServiceFactory.getInstance().getSystemInfoService().deleteUser(id);
	 }
	
	private Integer saveUserRoles()
	{
		//delete all user roles
		ServiceFactory.getInstance().getSystemInfoService().deleteUserRoles(userID);
		List<ModelUserRole> userRoles =  getInputUserRoles( this.roles);
		
		if (userRoles!=null)
		{
			for (ModelUserRole modelUserRole : userRoles) {
				ServiceFactory.getInstance().getSystemInfoService().insertUserRole(modelUserRole);
			}
		}
		return 0;
	}
	
	public String getRolesArray()
	{
 		List<ModelRole> rolesList = ServiceFactory.getInstance().getSystemInfoService().selectRoles();
 		
 		String result = "";
 		for (ModelRole modelRole : rolesList) {
			
 			if (result.length()>0 ) result += ",";
 			result += modelRole.getRoleName();
		}
 		roles = result;
		return Action.SUCCESS;
	}
	
	/*All roles name*/

	
	public List<ModelUserRole> getInputUserRoles(String roles)
	{
		List<ModelUserRole> userRoles = new ArrayList<ModelUserRole>();
		
		if ( roles.equals("" )) return null;
		String inputRoles[] = roles.split(",");

		List<ModelRole> rolesList = ServiceFactory.getInstance().getSystemInfoService().selectRoles();
 		
 		for (String  role : inputRoles) {
			 			
	 		for (ModelRole modelRole : rolesList) {
				
	 			if (modelRole.getRoleName().equals(role))
	 			{
	 				userRoles.add( new ModelUserRole(userID, modelRole.getRoleID()) );
	 			}
			}
 		
 		} 
	
		return userRoles;
	}
}
